package com.ape.mapsa.produccion.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.ape.mapsa.produccion.model.Formula;

@Repository
public class FormulasDAOImpl extends GenericDAOImpl<Formula, Long> implements FormulasDAO {

	@SuppressWarnings("unchecked")
	@Override
	public List<Formula> getFromulas(String query) {
		return sessionFactory.getCurrentSession().createCriteria(Formula.class)
				.add(Restrictions.like("description", query, MatchMode.ANYWHERE).ignoreCase()).list();
	}

	@Override
	public Formula getFormulaByDescription(String description) {
		return (Formula) sessionFactory.getCurrentSession().createCriteria(Formula.class)
				.add(Restrictions.eq("description", description).ignoreCase()).uniqueResult();
	}

	@Override
	public List<Formula> getAllFormulas() {
		Session session = sessionFactory.getCurrentSession();
		final Criteria crit = session.createCriteria(Formula.class);
		crit.setFetchMode("formulaMps", FetchMode.JOIN);
		crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		List<Formula> result = crit.list();
		return result;
	}

}
